<template>
  <div class="head-container">
    <!-- 搜索 -->
    <div v-show="sup_this.searchToggle">
      <el-select v-model="paramBean.searchScene" filterable clearable placeholder="搜索场景">
        <el-option
          v-for="(item) in dicts.crm_search_scene"
          :key="item.id"
          :label="item.dictName"
          :value="item.dictCode"/>
      </el-select>
      <el-input v-model="paramBean.name" clearable placeholder="输入线索名称搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="sup_this.toQuery"/>
      <el-select v-model="paramBean.customLevel" filterable clearable placeholder="客户等级">
        <el-option
          v-for="(item) in dicts.custom_level"
          :key="item.id"
          :label="item.dictName"
          :value="item.dictCode"/>
      </el-select>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="sup_this.toQuery">搜索</el-button>
      <el-button class="filter-item" size="mini" type="warning" icon="el-icon-refresh-left" @click="sup_this.resetQuery()">重置</el-button>
    </div>
    <div class="crud-opts">
      <ChangeCharger
        v-permission="['SYS_ADMIN','COMMON_ADMIN','CRM_CLUE_ALL','CRM_CLUE_CREATE','CRM_CLUE_SUBMIT','CRM_CLUE_DESTORY']"
        :sup_this="sup_this"
        @doMultiChangeCharger="doMultiChangeCharger">变更负责人</ChangeCharger>
      <el-button
        v-permission="['SYS_ADMIN','COMMON_ADMIN','CRM_CUSTOM_ALL','CRM_CUSTOM_CREATE','CRM_CUSTOM_SUBMIT','CRM_CUSTOM_DESTORY']"
        class="filter-item"
        size="mini"
        type="primary"
        icon="el-icon-user-solid"
        @click="toCustom">转化为客户</el-button>
      <el-button
        v-permission="['SYS_ADMIN','COMMON_ADMIN','CRM_CLUE_ALL','CRM_CLUE_CREATE']"
        class="filter-item"
        size="mini"
        type="primary"
        icon="el-icon-plus"
        @click="toAdd">新增</el-button>
      <el-button
        v-permission="['SYS_ADMIN','COMMON_ADMIN','CRM_CLUE_ALL','CRM_CLUE_SUBMIT']"
        v-loading="submitLoading"
        class="filter-item"
        size="mini"
        type="success"
        icon="el-icon-check"
        @click="doMultiSubmit">提交</el-button>
      <el-button
        v-permission="['SYS_ADMIN','COMMON_ADMIN','CRM_CLUE_ALL','CRM_CLUE_CREATE']"
        class="filter-item"
        size="mini"
        type="primary"
        icon="el-icon-document-copy"
        @click="toCopy">复制</el-button>
      <el-button
        v-permission="['SYS_ADMIN','COMMON_ADMIN','CRM_CLUE_ALL','CRM_CLUE_DELETE']"
        v-loading="deleteLoading"
        class="filter-item"
        size="mini"
        type="danger"
        icon="el-icon-delete"
        @click="doMultiDelete">删除</el-button>
      <el-button
        v-permission="['SYS_ADMIN','COMMON_ADMIN','CRM_CLUE_ALL','CRM_CLUE_CHANGE']"
        class="filter-item"
        size="mini"
        type="primary"
        icon="el-icon-edit-outline"
        @click="toChange">变更</el-button>
      <DestoryOpt
        v-permission="['SYS_ADMIN','COMMON_ADMIN','CRM_CLUE_ALL','CRM_CLUE_DESTORY']"
        v-loading="destoryLoading"
        :sup_this="sup_this"
        @doMultiDestory="doMultiDestory">注销</DestoryOpt>
      <DataGridOpts :sup_this="sup_this" @refreshClick="refreshClick" @searchClick="searchClick" />
    </div>
    <custom-form ref="customForm" :sup_this="sup_this" />
  </div>
</template>

<script>
import clue from '@/api/crm/Clue'
import ChangeCharger from '@/views/crm/clue/module/ChangeCharger'
import customForm from '@/views/crm/custom/module/form'

export default {
  components: { ChangeCharger, customForm },
  dictTypes: ['custom_level', 'custom_source_type', 'custom_trade', 'crm_search_scene'],
  props: {
    paramBean: {
      type: Object,
      required: true
    },
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      deleteLoading: false,
      submitLoading: false,
      destoryLoading: false
    }
  },
  methods: {
    refreshClick() {
      this.sup_this.init()
    },
    searchClick() {
      this.sup_this.toggleSearch()
    },
    beforeCreate() {
    },
    doMultiDelete() {
      const _this = this.sup_this
      _this.doMultiDelete(clue.deleteUrl(), this)
    },
    toCopy() {
      const _this = this.sup_this
      const selectMainId = _this.getSelectMainId()
      if (!selectMainId) {
        return
      }
      const _thisForm = _this.$refs.form
      _thisForm.initDataOfBefore()
      _thisForm.copyData(selectMainId)
    },
    doMultiSubmit() {
      const _this = this.sup_this
      _this.doMultiSubmit(clue.submitUrl(), this)
    },
    doMultiDestory(selectIds, isSubmit, busRmk) {
      const _this = this.sup_this
      _this.doMultiDestory(clue.destoryUrl(), selectIds, isSubmit, this, busRmk)
    },
    toChange() {
      const _this = this.sup_this
      const selectBusId = _this.getSelectBusId()
      if (!selectBusId) {
        return
      }
      const _thisForm = this.sup_this.$refs.form
      _thisForm.initDataOfBefore()
      _thisForm.changeData(selectBusId)
    },
    toAdd() {
      const _this = this.sup_this.$refs.form
      _this.initDataOfBefore()
      _this.addData()
    },
    doMultiChangeCharger(selectIds, newChargerId) {
      if (!newChargerId) {
        this.$message({
          message: '请选择责任人',
          type: 'warning'
        })
        return
      }
      const paramData = {
        idList: selectIds,
        paramBean: {
          chargerId: newChargerId
        }
      }
      const _this = this.sup_this
      clue.doChangeCharger(paramData).then(res => {
        _this.init()
      })
    },
    toCustom() {
      const _this = this.sup_this
      const busRelList = []
      let transferCustomFlag = false
      _this.selectedGridRows.forEach(row => {
        if (transferCustomFlag === 'Y' || row.selfState !== '10') {
          transferCustomFlag = true
          return
        }
        busRelList.push({ fromBusModule: row.busModule, fromBusClass: row.busClass, fromCode: row.sysGenCode, fromId: row.id })
      })
      if (transferCustomFlag) {
        this.$message({
          message: '请选择未转化客户的线索或有效记录',
          type: 'warning'
        })
        return
      }
      if (busRelList.length !== 1) {
        this.$message({
          message: '请选择一条记录',
          type: 'warning'
        })
        return
      }
      const _thisform = this.$refs.customForm
      _thisform.initDataOfBefore()
      _thisform.addDataFromClue(busRelList)
    }
  }
}
</script>
